<!-- 课程作业 -->
<template>
    <div class="course-assignment-container" v-loading="loading">
        <TaskList
            :data="homeworkList"
            :is-buy-person="isBuyPerson"
            :is-creater="isCreater"
            :is-preview="isPreview"
            @obtain-course="emit('obtain-course')"/>
    </div>
</template>

<script setup>
import TaskList from './task-list/index.vue';
import { ref, reactive, watch, nextTick } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute();

const props = defineProps({
    currentTab: {
        type: String,
        default: ''
    },
    detail: {
        type: Object,
        default: () => {
            return {};
        }
    },
    isBuyPerson: { // 是否是购买者
        type: Boolean,
        default: false
    },
    isCreater: { // 是否是创建者
        type: Boolean,
        default: false
    },
    isPreview: { // 是否是预览状态
        type: Boolean,
        default: false 
    }
})

const emit = defineEmits(['obtain-course']);

let currentTabKey = ref('CourseAssignment');

const loading = ref(false);

// 作业列表
const homeworkList = ref([])

// 获取作业列表
function getHomeList () {
    if (!route.params.moocId) {
        return false;
    }
    loading.value = true;
    G_transmit('moocHomeWorkList', {
        queries: {
            moocId: route.params.moocId
        }
    }).then((res) => {
        homeworkList.value = res.data || {};
    }).finally(() => {
        loading.value = false;
    })
}

watch(() => props.currentTab, async(newVal) => {
    await nextTick();
    if (newVal === currentTabKey.value) {
        getHomeList();
    }
}, {
    immediate: true
})
</script>

<style lang="scss" scoped>
.course-assignment-container {
    padding: 26px 20px 30px 20px;
}
</style>